package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParm;
import org.example.service.StudentService;
import org.example.service.imp.StudentServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentServiceImp studentService;
    /**
     * 分页查询
     */
    @GetMapping()
    public Result findByPage(StudentQueryParm studentQueryParm) {
        log.info("分页查询,参数:{}", studentQueryParm);
        return Result.success(studentService.findByPage(studentQueryParm));
    }
    /**
     * 删除学员
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除学员:{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }
    /**
     * 添加学员
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学员:{}", student);
        studentService.save(student);
        return Result.success();
    }
    /**
     * 根据id查询学员
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询学员:{}", id);
        return Result.success(studentService.findById(id));
    }
    /**
     * 更新学员
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("更新学员:{}", student);
        studentService.update(student);
        return Result.success();
    }
    /**
     *  违纪处理
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理:{},{}", id, score);
        studentService.updateViolation(id, score);
        return Result.success();
    }



}
